from app import app
from flask import render_template, request, jsonify
from app.controllers.chat_controller import main

# chat_bp = Blueprint('chat', __name__)

@app.route('/chat')
def chat_page():
    return render_template('chat.html')

@app.route('/chat', methods=['POST'])
def chat():
    data = request.json
    prompt = "你是一个具有深厚禅宗修养的大师，能够引用经典、翻译、以及历代大师的思想。你的言辞温和，充满禅意，能够帮助用户深入理解禅宗的哲学和实践。回答问题不要超过150字，不要用多级标题和分点回答。用户的提问如下："+data.get('prompt', '')
    if not prompt:
        return jsonify({"reply": "Prompt is required"}), 400

    # 调用对话 API
    appid = "628965d9"
    api_secret = "MzBiY2ZhNmU2ZDExNTBlMzE2ZWE1OTY1"
    api_key = "bd966d2d55a86fcfbacb176276d9c90e"
    Spark_url = "wss://spark-api.xf-yun.com/v3.5/chat"
    domain = "generalv3.5"

    # 获取对话结果
    try:
        response = main(appid, api_key, api_secret, Spark_url, domain, prompt)
        print(f"Response in chat view: {response}")  # 添加日志
        return jsonify({"reply": response})
    except Exception as e:
        print(f"Exception in chat view: {str(e)}")  # 添加日志
        return jsonify({"reply": str(e)}), 500

"""
@chat_bp.route('/chat', methods=['POST'])
def chat():
    data = request.json
    prompt = data.get('prompt', '')
    if not prompt:
        return jsonify({"reply": "Prompt is required"}), 400

    # 预设的回复
    preset_reply = "回答测试"
    return jsonify({"reply": preset_reply})
"""